package cn.quang.search.toolkit.component.groups;

import cn.quang.search.toolkit.component.ISearch;
import cn.quang.search.toolkit.component.SearchSecurtityUtil;
import cn.quang.search.toolkit.component.items.SearchItemFactory;
import cn.quang.search.toolkit.enums.DataSourceTypeEnum;
import cn.quang.search.toolkit.enums.SearchLogicType;
import cn.quang.search.toolkit.provider.entity.KeyOperatorValue;
import cn.quang.search.toolkit.provider.entity.QuerySlice;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class AbstractSearchGroup implements ISearchGroup {

    private final SearchLogicType logicType;
    protected final DataSourceTypeEnum dsType;
    protected final SearchItemFactory searchItemFactory;
    private final List<ISearch> items = new CopyOnWriteArrayList<>();
    private ISearch parentGroup;

    public AbstractSearchGroup(DataSourceTypeEnum dsType, SearchLogicType logicType){
        this.dsType = dsType;
        this.logicType = logicType;
        this.searchItemFactory = new SearchItemFactory(dsType);
    }

    public boolean isRoot(){
        return parentGroup == null;
    }

    @Override
    public boolean isLeaf() {
        return false;
    }

    @Override
    public ISearch getParentGroup() {
        return parentGroup;
    }

    @Override
    public void setParentGroup(ISearch parentGroup) {
        this.parentGroup = parentGroup;

        if(SearchSecurtityUtil.existsCirculateParent(this)){
            this.parentGroup = null;
            throw new IllegalArgumentException("设置parentGroup后，查询条件链存在死锁！");
        }
    }

    @Override
    public void addSearch(ISearch search){
        search.setParentGroup(this);
        items.add(search);
    }

    @Override
    public void addSearchGroup(ISearchGroup searchGroup) {
        this.addSearch(searchGroup);
    }

    @Override
    public List<ISearch> getSearchItems() {
        return items;
    }

    @Override
    public QuerySlice getQuerySlice() {
        if(items.isEmpty()){
            return null;
        }

        StringBuilder sqlTemplateBuilder = new StringBuilder();

        List<KeyOperatorValue> keyOperatorValues = new ArrayList<>();

        boolean isFirstItem = true;

        for(ISearch item : items){
            if(!isFirstItem){
                sqlTemplateBuilder.append(" ").append(logicType.getSqlConnector()).append(" ");
            }else{
                isFirstItem = false;
            }

            QuerySlice itemQuerySlice = item.getQuerySlice();
            sqlTemplateBuilder.append(itemQuerySlice.getSqlTemplate());
            List<KeyOperatorValue> itemKeyOperatorValues = itemQuerySlice.getKeyOperatorValues();
            if(CollectionUtils.isNotEmpty(itemKeyOperatorValues)) {
                keyOperatorValues.addAll(itemKeyOperatorValues);
            }
        }

        if(items.size() > 1){
            // 该SearchGroup包含至少2个item，需要加括号
            sqlTemplateBuilder.insert(0,"(").append(")");
        }

        return QuerySlice.of(
                sqlTemplateBuilder.toString(),
                keyOperatorValues);
    }
}
